package com.example.accessingdatamapper;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import tk.mybatis.spring.annotation.MapperScan;

/**
 * @author caiwl
 * @date 2020/12/24 9:29
 */
@MapperScan("com.example.accessingdatamapper")
@SpringBootApplication
public class AccessingDataMapperApplication {

    private static final Logger log = LoggerFactory.getLogger(AccessingDataMapperApplication.class);

    public static void main(String[] args) {
        SpringApplication.run(AccessingDataMapperApplication.class, args);
    }

    @Bean
    public CommandLineRunner demo(CustomerMapper mapper) {
        return (args) -> {
            // save a few customers
            mapper.insertSelective(new Customer("Jack", "Bauer"));
            mapper.insertSelective(new Customer("Chloe", "O'Brian"));
            mapper.insertSelective(new Customer("Kim", "Bauer"));
            mapper.insertSelective(new Customer("David", "Palmer"));
            mapper.insertSelective(new Customer("Michelle", "Dessler"));

            // fetch all customers
            log.info("Customers found with selectAll():");
            log.info("-------------------------------");
            mapper.selectAll().forEach(customer -> log.info(customer.toString()));
            log.info("");

            // fetch an individual customer by ID
            Customer customer = mapper.selectByPrimaryKey(1L);
            log.info("Customer found with selectByPrimaryKey(1L):");
            log.info("--------------------------------");
            log.info(customer.toString());
            log.info("");

            // fetch customers by last name
            log.info("Customer found with selectByLastName('Bauer'):");
            log.info("--------------------------------------------");
            mapper.selectByLastName("Bauer").forEach(bauer -> log.info(bauer.toString()));
            // for (Customer bauer : mapper.findByLastName("Bauer")) {
            //  log.info(bauer.toString());
            // }
            log.info("");
        };
    }
}
